<?php
/**
 * Created by PhpStorm.
 * User: FDG296
 * Date: 2017/7/10
 * Time: 14:56
 */
namespace backend\controllers;

use Yii;
use yii\web\AdminController;
use backend\models\UserDepartment;
use common\helps;

class SysDepartmentController extends AdminController{
    public $layout = false;
    //业务关键字
    public $service = 'SysDepartment';
    /**
     * 组织机构
     */
    public function actionIndex(){

        //列表
        $data_list = UserDepartment::find()->orderBy(array('sort'=>SORT_ASC))->asArray()->all();

        foreach($data_list as &$value){
            $value['html'] = '';
        }

        $list_tree = \common\helps\tools::getCustomerTree($data_list,0,0);

        //modal中的部门树状图
        $list = $this->getDepartTree();
        $this->saveLog($this->service,'组织机构管理页','VIEW','');                     //日志
        return $this->render('index',['tree_list'=>\yii\helpers\Json::encode($list),'data_list'=>$list_tree]);
    }

    public function getDepartTree(){
        $list = UserDepartment::find()->where(array('use_flg'=>UserDepartment::STATUS_ENABLE))->orderBy(array('sort'=>SORT_ASC))->asArray()->all();
        $tree_list = array();
        $tree_list[0]['id'] = '';
        $tree_list[0]['pId'] = '';
        $tree_list[0]['name'] = '顶级部门';
        foreach($list as $key=>$value){
            $tree_list[$key+1]['id'] = $value['id'];
            $tree_list[$key+1]['pId'] = $value['parent_id'];
            $tree_list[$key+1]['name'] = $value['dept_name'];
        }
        return $tree_list;
    }

    /**
     * 新增部门
     */
    public function actionAddDepart(){
        $model = new UserDepartment();
        if($model->validate(Yii::$app->request->post())){
            $data = Yii::$app->request->post();
            $data_now['dept_name'] = $data['dept_name'];
            $data_now['memo'] = $data['memo'];
            $data_now['sort'] = empty($data['sort']) ? 1 : $data['sort'];;
            $data_now['parent_id'] = $data['parent_id'] == '' ? 0 : $data['parent_id'];
            $data_now['use_flg'] = $data['use_flg'];
            $data_now['create_time'] = date('Y-m-d H:i:s',time());
            $data_now['creator'] = $model->getCreator();
            $res = Yii::$app->db->createCommand()->insert('j_user_department',$data_now)->execute();
            if($res > 0){
                $this->saveLog($this->service,'新增部门','ADD', '新增：'.  $data_now['dept_name'] . "\r\n" .\yii\helpers\Json::encode($data_now));                     //日志
                $this->retJson('添加成功',200);
            }else{
                $this->retJson('添加失败',500);
            }
        }else{
            $this->retJson($model->errors,500);
        }

    }

    /**
     * 禁用部门
     */
    public function actionDisable(){
        $model = new UserDepartment();
        $data = Yii::$app->request->post();

        $mod_data['use_flg'] = 0;
        $mod_data['editor'] = $model->getCreator();
        $mod_data['edit_time'] = date('Y-m-d H:i:s',time());
        /*$model::updateAll(['use_flg' => 0], ['or','id'=> $data['id'],'parent_id'=>$data['id']]);*/
        $res = $model::updateAll ($mod_data, " id = ".$data['id']." or parent_id = " . $data['id']);

        if($res > 0){
            $this->saveLog($this->service,'禁用部门','USEFLG0','ID = ' .$data['id']);                     //日志
            $this->retJson('部门禁用成功',200);
        }else{
            $this->retJson('部门禁用失败',500);
        }
    }

    /**
     * 启用部门
     */
    public function actionEnable(){
        $model = new UserDepartment();
        $data = Yii::$app->request->post();

        $mod_data['use_flg'] = 1;
        $mod_data['editor'] = $model->getCreator();
        $mod_data['edit_time'] = date('Y-m-d H:i:s',time());

        /*$model::updateAll(['use_flg' => 0], ['or','id'=> $data['id'],'parent_id'=>$data['id']]);*/
        $res = $model::updateAll ($mod_data, " id = ".$data['id']);

        if($res > 0){
            $this->saveLog($this->service,'启用部门','USEFLG1','ID = ' .$data['id']);                     //日志
            $this->retJson('部门启用成功',200);
        }else{
            $this->retJson('部门启用失败',500);
        }
    }

    /*
     * 删除部门
     */
    public function actionDelDepart(){
        $model = new UserDepartment();
        $data = Yii::$app->request->post();

        $res = $model::deleteAll(" id = ".$data['id']." or parent_id = " . $data['id']);

        if($res > 0){
            $this->saveLog($this->service,'删除部门','DEL','ID = ' .$data['id']);                     //日志
            $this->retJson('部门删除成功',200);
        }else{
            $this->retJson('部门删除失败',500);
        }
    }

    /**
     * 修改部门
     */
    public function actionModDepart(){
        $model = new UserDepartment();
        $data = Yii::$app->request->post();
        $data['edit_time'] = date('Y-m-d H:i:s',time());

        $data['editor'] = $model->getCreator();
        /*$model::updateAll(['use_flg' => 0], ['or','id'=> $data['id'],'parent_id'=>$data['id']]);*/
        $res = $model::updateAll ($data, " id = ".$data['id']);

        if($res > 0){
            $this->saveLog($this->service,'修改部门','MOD', $data['dept_name'] . "\r\n" .\yii\helpers\Json::encode($data));                     //日志
            $this->retJson('部门修改成功',200);
        }else{
            $this->retJson('部门修改失败',500);
        }
    }

    /**
     * 局部关闭csrf
     * action 方法名
     */
    public function beforeAction($action){

        $currentaction = $action->id;

        $novalidactions = ['disable','enable','del-depart','mod-depart'];

        if(in_array($currentaction,$novalidactions)) {

            $action->controller->enableCsrfValidation = false;

        }
        parent::beforeAction($action);

        return true;
    }
}